/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

/**
 *
 * @author Administrator
 */
public class MainGameCanvas extends GameCanvas implements Runnable, PlayerListener {

    //midlet
    private MainMIDlet midlet;
    private int xBall = 0;
    private int yBall = 0;
    private boolean running = false;
    private Sprite spBall;
    private LayerManager lmgr;
    //speed
    private int speedGame = 200;
    //for sound
    private long delay;
    private Player sound01;

    public MainGameCanvas(MainMIDlet midlet) {
        super(false);
        setFullScreenMode(true);
        this.midlet = midlet;
        try {
            //animated sprite
            spBall = new Sprite(Image.createImage("/ball.png"), 50, 50);
            xBall = 10;
            yBall = 10;
            spBall.setPosition(xBall, yBall);

            lmgr = new LayerManager();
            lmgr.append(spBall);
        } catch (Exception e) {
        }

        try {
            sound01 = Manager.createPlayer(getClass().getResourceAsStream("/town.mid"),
                    "audio/midi");
            sound01.realize();
            sound01.prefetch();
            sound01.addPlayerListener(this);
            sound01.setLoopCount(-1);
            sound01.start();
        } catch (Exception e) {
        }
    }

    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
        try {
            sound01.stop();
        } catch (MediaException ex) {
        }
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        spBall.nextFrame();
        moveBall();
        spBall.setPosition(xBall, yBall);

        //paint all layers
        lmgr.paint(g, 0, 0);
        flushGraphics();
    }

    private void moveBall() {
        if (xBall < getWidth()) {
            xBall = xBall + 5;
        } else {
            xBall = xBall - 5;
        }
    }

    protected void keyPressed(int keyCode) {
        String statusKey = String.valueOf(keyCode);
        System.out.println(statusKey);

        if (keyCode == -5) { //fire
        } else if (keyCode == -2) { //down
        } else if (keyCode == -4) { //right
        } else if (keyCode == -1) { //up
        } else if (keyCode == -3) { //left            
        }
        repaint();
    }

    public void playerUpdate(Player arg0, String eventType, Object arg2) {
        if (eventType.equals(STARTED)) {
            delay = System.currentTimeMillis() - delay;
            repaint();
        } else if (eventType.equals(END_OF_MEDIA)) {
            delay = System.currentTimeMillis();
        }
    }
}
